﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using PlanB.ShareDove.ConferenceOrganization.ConsumerLayer.WinPhoneClient.ConferenceSessionsService;
using System.Data.Services.Client;


namespace PlanB.ShareDove.ConferenceOrganization.ConsumerLayer.WinPhoneClient
{
    /// <summary>
    /// Main view model class (all items)
    /// </summary>
    public class MainViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Main view model constructor
        /// </summary>
        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();
        }


        /// <summary>
        /// Data about conference sessions retrieved from the REST service
        /// </summary>
        public DataServiceCollection<RestSession> RestData = null;

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }

        
        /// <summary>
        /// Is data loaded in view
        /// </summary>
        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            this.Items.Clear();

            if (RestData != null)
            {
                foreach (RestSession session in RestData)
                {
                    this.Items.Add(
                        new ItemViewModel()
                        {
                            LineOne = session.Title,
                            LineTwo = string.Format("{0} {1} / {2} / {3}", session.Slot.ToShortDateString(), session.Slot.ToShortTimeString(), session.TrackName, session.RoomName),
                            LineThree = session.Abstract
                        }
                        );
                }
            }


            this.IsDataLoaded = true;
        }

        /// <summary>
        /// Property Changed event
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}